
﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Baocao;
using System.IO;
using LPV_POS.Baocao.Ellic;
using CrystalDecisions.CrystalReports.Engine;
using LPV_POS.Baocao.Bixolon;

namespace LPV_POS.mh_pos.mh_inbaocao
{
    public partial class mh_viewthanhtoan : DevExpress.XtraEditors.XtraForm
    {
        DataTable tb = null;
        DataTable _tbPhieu = null;

        //string sPhieu = "";
        string sPhieuCT = "";
       


        public mh_viewthanhtoan()
        {
            InitializeComponent();
        }

        private DataTable get_Logo()
        {
            DataTable dt = new DataTable();
            DataRow row;
            dt.Columns.Add("Hinh", System.Type.GetType("System.Byte[]"));
            dt.Columns.Add("HinhDetail", System.Type.GetType("System.Byte[]"));
            row = dt.NewRow();

            //Create Filestream to read image
            FileStream fs;

            //Create variable BinaryReader to read bytes of image
            BinaryReader br;

            //Check exists of image

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "logo1.bmp"))
            {
                //open image in file stream
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "logo1.bmp", FileMode.Open);
            }
            else //if image not exists show image Nopoto.img
                return dt;

            br = new BinaryReader(fs);
            byte[] b = new byte[fs.Length];
            b = br.ReadBytes(Convert.ToInt32(fs.Length));
            row["Hinh"] = b;
            row["HinhDetail"] = b;
            dt.Rows.Add(row);
            fs.Dispose();


            return dt;
        }

        #region "   getTable_Phieu"
        private DataTable getTable_Phieu(string maBan, string maPhieu)
        {
            string sSQL = "";
            if (maBan != "")
                maBan = " AND P.DM_BAN_MA LIKE '" + maBan + "' ";
            if (maPhieu != "")
                maPhieu = " AND p.OBJID LIKE '" + maPhieu + "'";

            sSQL = "SELECT TOP 1 b.OBJNAME, p.OBJID, p.DM_BAN_MA, NGUOI_LAP = ISNULL(n.OBJNAME, p.NGUOI_LAP), p.NGAY_GIO_VAO, p.NGAY_GIO_RA, " +
                   "        p.TINH_TIEN_THEO_NGAY, p.TIEN_GIO, THANH_TIEN = p.THANH_TIEN, p.CK_PHANTRAM, p.CK_TIEN, " +
                   "        CK_THUCAN_PHANTRAM = ISNULL(p.CK_THUCAN_PHANTRAM, 0), CK_THUCAN_TIEN = ISNULL(p.CK_THUCAN_TIEN, 0), " +//20111110
                   "        CK_THUCUONG_PHANTRAM = ISNULL(p.CK_THUCUONG_PHANTRAM, 0), CK_THUCUONG_TIEN = ISNULL(p.CK_THUCUONG_TIEN, 0), " +//20111110

                   "        p.VAT_PHANTRAM, p.VAT_TIEN, p.TTIEP_PHANTRAM, p.TTIEP_TIEN, " +
                   "        p.PHU_THU_PHANTRAM, p.PHU_THU, p.PHU_THU_KHAC, p.TONG_TIEN, p.GHI_CHU, " +
                   "        NGAYVAO = CONVERT(VARCHAR(30), p.NGAY_GIO_VAO, 103) +' '+ CONVERT(VARCHAR(30), p.NGAY_GIO_VAO, 108), " +
                   "        NGAYRA = CONVERT(VARCHAR(30), p.NGAY_GIO_RA, 103) +' '+ CONVERT(VARCHAR(30), p.NGAY_GIO_RA, 108), " +
                   "        TIEN_DA_NHAN = p.TIEN_DA_NHAN, " +
                   "        DAT_COC = ISNULL((SELECT  DAT_COC FROM DM_BAN_DAT_BAN WHERE BH_PHIEU_ID = p.OBJID)  ,0), " +
                   "        KM_TIEN, " +
                   "        DM_KHACHHANG_TEN = dk.OBJNAME, " +
                   "        dk.NGUOILIENHE, " +
                   "        DM_KHACHHANG_DTB = dk.DIENTHOAIBAN, " +
                   "        DM_KHACHHANG_DTDD = dk.DIENTHOAIDD, " +
                   "        P.KM_TIEN, P.KM_PHANTRAM, P.NHANVIEN_PHUCVU, NGAY_LAM_VIEC = CONVERT(VARCHAR(30), p.NGAY_LAM_VIEC, 103) " +
                   "FROM BH_PHIEU p " +
                   "        LEFT JOIN DM_BAN b ON p.DM_BAN_MA = b.OBJID " +
                   "        LEFT JOIN DM_DANGNHAP d ON p.NGUOI_LAP = d.USERNAME " +
                   "        LEFT JOIN DM_NHANVIEN n ON d.MA_NHANVIEN = n.OBJID " +
                   "        LEFT JOIN DM_KHACHHANG dk ON p.CUSTOMER_ID = dk.THEKHACHHANG " +
                   "WHERE 1 = 1 " + maBan + maPhieu
                ;
            DataTable _tbPhieu = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            return _tbPhieu;
        }
        #endregion

        public DataTable f_getchitietphieu(string _maPhieu)
        {
            #region "   LoadData BH_PHIEU-BH_PHIEU_CT"
            tb = null;
            _tbPhieu = null;

            //  Variable.Get_DM_MAYIN_BIENNHAN();

            DataRow _drPhieu = null;
            DataTable _tbPhieuCT = null;
            _tbPhieu = this.getTable_Phieu("", _maPhieu + "");
            string maPhieu = "";

            if (_tbPhieu.Rows.Count == 1)
            {
                maPhieu = _tbPhieu.Rows[0]["OBJID"] + "";

                if (_tbPhieu.Select("OBJID LIKE '" + maPhieu + "'").Length > 0)
                    _drPhieu = _tbPhieu.Select("OBJID LIKE '" + maPhieu + "'")[0];
                string sSQL = "";

                string sTien_Gio = "Tiền giờ";
                if (Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''").Length > 0)
                    sTien_Gio = Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''")[0]["TIEN_PHONG_GIO"] + "";

                sSQL =
                    "SELECT MIN(aa.OBJID), aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, SO_LUONG = SUM(aa.SO_LUONG), aa.DVT, DON_GIA = aa.DON_GIA, " +
                    "       aa.CHIETKHAU_PHANTRAM, " +
                    "       CHIETKHAU_TIEN = MAX(aa.CHIETKHAU_TIEN), " +
                    "       THANH_TIEN = SUM(aa.THANH_TIEN),  " +
                    "       aa.VAT_PHANTRAM, VAT_TIEN = SUM(aa.VAT_TIEN), " +
                    "       TEN_DVT = ISNULL(aa.TEN_DVT,'') " +
                    "FROM  " +
                    "(  " +
                    "    SELECT b.OBJID, OBJNAME = (CASE WHEN b.MAHH LIKE 'Tien_Gio%' THEN N'" + sTien_Gio + "' ELSE h.OBJNAME END ), b.BH_PHIEU_MA, " +
                    "           b.MAHH, b.SO_LUONG, b.DVT, b.DON_GIA, b.CHIETKHAU_PHANTRAM, " +
                    "           b.CHIETKHAU_TIEN, THANH_TIEN = b.THANH_TIEN, b.VAT_PHANTRAM, b.VAT_TIEN, " +
                    "           TEN_DVT = dd.OBJNAME " +
                    "    FROM BH_PHIEU_CT b " +
                    "        LEFT JOIN DM_HH h ON b.MAHH = h.OBJID  " +
                    "        LEFT JOIN DM_DVT dd ON dd.OBJID = b.DVT " +
                    "    WHERE b.BH_PHIEU_MA LIKE '" + maPhieu + "'  " +
                    ")aa " +
                    "GROUP BY aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, aa.DVT, aa.VAT_PHANTRAM, aa.DON_GIA, aa.CHIETKHAU_PHANTRAM, aa.TEN_DVT " +
                    //"HAVING SUM(aa.SO_LUONG) > 0 " +
                    "ORDER BY MIN(aa.OBJID)";

                _tbPhieuCT = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            }
            return _tbPhieuCT;
            #endregion
        }

        private void f_inbiennhan(string maphieu, Boolean _bInlai, int i1Epson_2Bixolon_3Bixolon100, string sTieuDe, int iPrintOut)
        {//int i1Epson_2Bixolon_3Bixolon100
            try
            {
                this.Cursor = Cursors.AppStarting;
                ReportDocument rpt = null;

                _bInlai = In.Print_InLai_HoaDon.inlai;//----------16/05/2012-----------

                //if (epson == true)  // may in epson
                //    rpt = new bc_bill_thanhtoan();
                //else
                //    rpt = new bc_bill_thanhtoan_Bixolon(); // may in bixolon


                switch (i1Epson_2Bixolon_3Bixolon100)
                {
                    case 1:
                    case 2:
                        rpt = new ReportDocument();
                        string sRptPath = Application.StartupPath + "\\report\\bc_bill_thanhtoan_Bixolon.rpt";
                        rpt.Load(sRptPath, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);
                        break;
                    case 3:
                        rpt = new ReportDocument();
                        sRptPath = Application.StartupPath + "\\report\\bc_bill_thanhtoan_Bixolon100.rpt";
                        rpt.Load(sRptPath, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);
                        break;

                }

                rpt.DataDefinition.FormulaFields["tieude"].Text = "'" + sTieuDe + "'";

                #region "chỉnh bill thanh toán với thanh toán nhiều hình thức , editor: PL, date: 2012.08.10"

                if (_bInlai == false)
                {
                    if (mh_banhang9_TienMat_The.hinhthucthanhtoan != "" && mh_banhang9_TienMat_The.hinhthucthanhtoan != "Nợ")
                    {
                        if (mh_banhang9_TienMat_The.hinhthucthanhtoan == "Tiền mặt")
                        {
                            rpt.DataDefinition.FormulaFields["f_kieuthanhtoan"].Text = "'Tiền mặt'";
                        }
                        else
                            rpt.DataDefinition.FormulaFields["f_kieuthanhtoan"].Text = "'" + mh_banhang9_TienMat_The.tenthe + "'";
                    }
                    else 
                    {
                        rpt.DataDefinition.FormulaFields["f_kieuthanhtoan"].Text = "'Yes'";
                    }
                }
                else
                {
                    DataTable dt_temp1 = f_Get_TINHTIENBANGTHE(maphieu);
                    string str;
                    int flag = 0;

                    if (dt_temp1.Rows.Count > 0)
                    {
                        str = dt_temp1.Rows[0]["TINH_TIEN_BANG_THE"].ToString();
                        int.TryParse(str.Trim(), out flag);

                    }

                    if (flag == 1)
                    {
                        DataTable dt_temp = f_GetOneRow(maphieu);
                        if (dt_temp.Rows.Count > 0)
                        {
                            string st_tenthe = dt_temp.Rows[0]["OBJNAME_THE"].ToString();
                            rpt.DataDefinition.FormulaFields["f_kieuthanhtoan"].Text = "'" + st_tenthe + "'";
                        }
                    }
                    else if (flag == 0)
                    {
                        rpt.DataDefinition.FormulaFields["f_kieuthanhtoan"].Text = "'Tiền mặt'";
                    }
                    else if (flag == 2)
                    {
                        rpt.DataDefinition.FormulaFields["f_kieuthanhtoan"].Text = "'Yes'";
                    }
                }

                #endregion


                // in header bill
                decimal tongtien;
                decimal tienkhachdua;
                decimal tiendatcoc;
                Boolean in_sohoadon = false;
                Boolean in_tendonvi = false;
                Boolean in_dienthoaidonvi = false;
                Boolean in_diachidonvi = false;
                Boolean in_logo = false;


                DataTable tbDM_MAYIN_BIENNHAN_CAUHINH = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
                                                                                        new string[] { "@sSQL" },
                                                                                        new object[] { "SELECT * FROM DM_MAYIN_BIENNHAN_CAUHINH" });

                cpublic ocpublic = new cpublic();

                if (tbDM_MAYIN_BIENNHAN_CAUHINH.Rows.Count > 0)
                {
                    #region
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_ORDER_NO"] + "", out in_sohoadon);
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_STORE_NAME"] + "", out in_tendonvi);
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_PHONE_NUMBER"] + "", out in_dienthoaidonvi);
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_STORE_ADDRESS"] + "", out in_diachidonvi);
                    Boolean.TryParse(tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PRINT_LOGO"] + "", out in_logo);

                    CrystalDecisions.CrystalReports.Engine.Section sec_logo = (CrystalDecisions.CrystalReports.Engine.Section)rpt.ReportDefinition.Sections["Section1"];
                    if (in_logo)
                    {
                        DataTable dt = get_Logo();
                        if (dt.Rows.Count > 0)
                            rpt.Subreports[0].SetDataSource(dt);
                        sec_logo.SectionFormat.EnableSuppress = dt.Rows.Count <= 0;
                    }
                    else
                    {
                        sec_logo.SectionFormat.EnableSuppress = true;
                    }


                    if (in_tendonvi == true)
                    {
                        //rpt.Section2.SectionFormat.EnableSuppress = false;
                        rpt.DataDefinition.FormulaFields["Sec2"].Text = "'" + "" + "'";
                    }
                    else
                    {
                        //  rpt.Section2.SectionFormat.EnableSuppress = true;
                        rpt.DataDefinition.FormulaFields["Sec2"].Text = "'" + "x" + "'";
                    }
                    CrystalDecisions.CrystalReports.Engine.TextObject str_congty = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tencongty"];
                    str_congty.Text = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["HEADER"] + "";
                    if (in_dienthoaidonvi == true)
                    {
                        //rpt.Section3.SectionFormat.EnableSuppress = false;
                        rpt.DataDefinition.FormulaFields["Sec3"].Text = "'" + "" + "'";
                    }
                    else
                    {

                        //rpt.Section3.SectionFormat.EnableSuppress = true;
                        rpt.DataDefinition.FormulaFields["Sec3"].Text = "'" + "x" + "'";
                    }
                    CrystalDecisions.CrystalReports.Engine.TextObject str_diachicongty;
                    str_diachicongty = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_diachicongty"];
                    str_diachicongty.Text = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["ADDRESS"] + "";

                    if (in_diachidonvi == true)
                    {
                        //rpt.Section4.SectionFormat.EnableSuppress = false;
                        rpt.DataDefinition.FormulaFields["Sec4"].Text = "'" + "" + "'";
                    }
                    else
                    {
                        //   rpt.Section4.SectionFormat.EnableSuppress = true;
                        rpt.DataDefinition.FormulaFields["Sec4"].Text = "'" + "x" + "'";
                    }


                    CrystalDecisions.CrystalReports.Engine.TextObject str_dienthoai;
                    str_dienthoai = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_dienthoaicongty"];
                    str_dienthoai.Text = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["PHONE"] + "";

                    //if (ocpublic.Report_Exist_Object(rpt, "txt_baocaodoanhthu"))
                    //{
                    //    CrystalDecisions.CrystalReports.Engine.TextObject txt_baocaodoanhthu;
                    //    txt_baocaodoanhthu = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_baocaodoanhthu"];
                    //    txt_baocaodoanhthu.Text = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["TITLE"] + "";
                    //}
                    #endregion
                }

                tb = f_getchitietphieu(maphieu);

                if (tb == null)
                    return;
                


                if (_tbPhieu != null || _tbPhieu.Rows.Count > 0)
                {
                    #region
                    if (_bInlai == true)
                        rpt.DataDefinition.FormulaFields["inlai"].Text = "'ABC'";//2011.11.28                        
                    else
                        rpt.DataDefinition.FormulaFields["inlai"].Text = "''";//2011.11.28

                    string sKH_Ten = _tbPhieu.Rows[0]["DM_KHACHHANG_TEN"] + "";
                    if (sKH_Ten == "")
                        sKH_Ten = _tbPhieu.Rows[0]["NGUOILIENHE"] + "";

                    if (sKH_Ten != "")
                    {
                        mh_dmkhachhang omh_dmkhachhang = new mh_dmkhachhang();
                        string skhachhang_op = ocpublic.Registry_Get(omh_dmkhachhang.Name + omh_dmkhachhang.txtkhachhang_optioninbill.Name);
                        sKH_Ten = skhachhang_op.Trim() + ": " + sKH_Ten;
                        string sKH_Sdt = _tbPhieu.Rows[0]["DM_KHACHHANG_DTDD"] + "";
                        if (sKH_Sdt != "")
                            sKH_Sdt = "Đt: " + sKH_Sdt;
                        rpt.DataDefinition.FormulaFields["KH_SDT"].Text = "'" + sKH_Sdt + "'";//2011.05.17
                    }
                    rpt.DataDefinition.FormulaFields["KH_TEN"].Text = "'" + sKH_Ten + "'";//2011.05.17

                    //
                    CrystalDecisions.CrystalReports.Engine.TextObject str_ban;
                    str_ban = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_ban"];
                    str_ban.Text = Variable.tbDM_LOAIDICHVU.Select("Isrun=true")[0]["BAN_PHONG"] + ": " + _tbPhieu.Rows[0]["OBJNAME"] + "";

                    rpt.DataDefinition.FormulaFields["fngay1"].Text = "'" + "Vào: " + _tbPhieu.Rows[0]["NGAYVAO"] + "'";//2011.11.28                    
                    if (ocpublic.Registry_Get("rbtHienThiGioRa") == "True")
                        rpt.DataDefinition.FormulaFields["fngay2"].Text = "'" + "Ra: " + _tbPhieu.Rows[0]["NGAYRA"] + "'";//2011.11.28

                    CrystalDecisions.CrystalReports.Engine.TextObject str_nhanvien;
                    str_nhanvien = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_nhanvien"];
                    str_nhanvien.Text = str_nhanvien.Text.Trim() + " " + _tbPhieu.Rows[0]["NGUOI_LAP"];

                    rpt.DataDefinition.FormulaFields["ghi_chu"].Text = "'" + _tbPhieu.Rows[0]["GHI_CHU"] + "'";

                    CrystalDecisions.CrystalReports.Engine.TextObject str_tongtien;
                    str_tongtien = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tongtien"];
                    str_tongtien.Text = decimal.Parse(_tbPhieu.Rows[0]["THANH_TIEN"] + "").ToString("N0");
                    rpt.DataDefinition.FormulaFields["ftongtien"].Text = "'" + str_tongtien.Text + "'";

                    rpt.DataDefinition.FormulaFields["fNVPV"].Text = "'" + _tbPhieu.Rows[0]["NHANVIEN_PHUCVU"] + "'";
                    //rpt.DataDefinition.FormulaFields["NGAY_LAM_VIEC"].Text = "'" + _tbPhieu.Rows[0]["NGAY_LAM_VIEC"] + "'";

                    // CrystalDecisions.CrystalReports.Engine.TextObject str_giamgia;
                    //str_giamgia = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_giamgia"];
                    //str_giamgia.Text = decimal.Parse(_tbPhieu.Rows[0]["CK_TIEN"] + "").ToString("N0");
                    if (decimal.Parse(_tbPhieu.Rows[0]["CK_TIEN"] + "") > 0)
                        rpt.DataDefinition.FormulaFields["fgiamgia"].Text = "'-" + decimal.Parse(_tbPhieu.Rows[0]["CK_TIEN"] + "").ToString("N0") + "'";
                    else
                        rpt.DataDefinition.FormulaFields["fgiamgia"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["CK_TIEN"] + "").ToString("N0") + "'";

                    #region "   CK_THUCAN_TIEN"
                    if (decimal.Parse(_tbPhieu.Rows[0]["CK_THUCAN_TIEN"] + "") > 0)
                        rpt.DataDefinition.FormulaFields["fgiamgia_an"].Text = "'-" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCAN_TIEN"] + "").ToString("N0") + "'";
                    else
                        rpt.DataDefinition.FormulaFields["fgiamgia_an"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCAN_TIEN"] + "").ToString("N0") + "'";
                    //
                    if (decimal.Parse(_tbPhieu.Rows[0]["CK_THUCAN_PHANTRAM"] + "") > 0)
                        rpt.DataDefinition.FormulaFields["fgiamgia_anPT"].Text = "'-" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCAN_PHANTRAM"] + "").ToString("N0") + "'";
                    else
                        rpt.DataDefinition.FormulaFields["fgiamgia_anPT"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCAN_PHANTRAM"] + "").ToString("N0") + "'";
                    #endregion

                    #region "   CK_THUCUONG_TIEN"
                    if (decimal.Parse(_tbPhieu.Rows[0]["CK_THUCUONG_TIEN"] + "") > 0)
                        rpt.DataDefinition.FormulaFields["fgiamgia_uong"].Text = "'-" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCUONG_TIEN"] + "").ToString("N0") + "'";
                    else
                        rpt.DataDefinition.FormulaFields["fgiamgia_uong"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCUONG_TIEN"] + "").ToString("N0") + "'";
                    //
                    if (decimal.Parse(_tbPhieu.Rows[0]["CK_THUCUONG_PHANTRAM"] + "") > 0)
                        rpt.DataDefinition.FormulaFields["fgiamgia_uongPT"].Text = "'-" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCUONG_PHANTRAM"] + "").ToString("N0") + "'";
                    else
                        rpt.DataDefinition.FormulaFields["fgiamgia_uongPT"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["CK_THUCUONG_PHANTRAM"] + "").ToString("N0") + "'";
                    #endregion


                    CrystalDecisions.CrystalReports.Engine.TextObject str_giamgiapt;
                    str_giamgiapt = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_giamgiapt"];
                    str_giamgiapt.Text = "(" + decimal.Parse(_tbPhieu.Rows[0]["CK_PHANTRAM"] + "").ToString("N0") + "%)";
                    if (str_giamgiapt.Text.Trim() == "(0%)")
                        str_giamgiapt.Text = "";

                    //CrystalDecisions.CrystalReports.Engine.TextObject str_tientip;
                    //str_tientip = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tientip"];
                    //str_tientip.Text = decimal.Parse(_tbPhieu.Rows[0]["TTIEP_TIEN"] + "").ToString("N0");
                    rpt.DataDefinition.FormulaFields["ftientip"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["TTIEP_TIEN"] + "").ToString("N0") + "'";

                    CrystalDecisions.CrystalReports.Engine.TextObject str_tientip_phantram;
                    str_tientip_phantram = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tientip_pt"];
                    str_tientip_phantram.Text = "(" + decimal.Parse(_tbPhieu.Rows[0]["TTIEP_PHANTRAM"] + "").ToString("N0") + "%)";
                    if (str_tientip_phantram.Text.Trim() == "(0%)")
                        str_tientip_phantram.Text = "";

                    #region "   KHUYEN MAI"
                    if (ocpublic.Report_Exist_Object(rpt, "txtKM_Tien"))//9999
                    {
                        if (_tbPhieu.Rows[0]["KM_PHANTRAM"] + "" != "" && _tbPhieu.Rows[0]["KM_PHANTRAM"] + "" != "0")
                            rpt.DataDefinition.FormulaFields["KM_PTRAM"].Text = "'(" + _tbPhieu.Rows[0]["KM_PHANTRAM"] + "%)'";

                        if (_tbPhieu.Rows[0]["KM_TIEN"] + "" != "" && _tbPhieu.Rows[0]["KM_TIEN"] + "" != "0")
                        {
                            float fKM_TIEN = 0;
                            float.TryParse(_tbPhieu.Rows[0]["KM_TIEN"] + "", out fKM_TIEN);
                            rpt.DataDefinition.FormulaFields["KM_TIEN"].Text = "'-" + fKM_TIEN.ToString("N0") + "'";
                        }
                    }
                    #endregion

                    //CrystalDecisions.CrystalReports.Engine.TextObject str_thuedichvu;
                    //str_thuedichvu = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_thuedichvu"];
                    //str_thuedichvu.Text = decimal.Parse(_tbPhieu.Rows[0]["PHU_THU"] + "").ToString("N0");
                    rpt.DataDefinition.FormulaFields["fthuedichvu"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["PHU_THU"] + "").ToString("N0") + "'";

                    CrystalDecisions.CrystalReports.Engine.TextObject str_phidichvu_pt;
                    str_phidichvu_pt = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_phidichvu_pt"];
                    str_phidichvu_pt.Text = "(" + decimal.Parse(_tbPhieu.Rows[0]["PHU_THU_PHANTRAM"] + "").ToString("N0") + "%)";
                    if (str_phidichvu_pt.Text.Trim() == "(0%)")
                        str_phidichvu_pt.Text = "";

                    CrystalDecisions.CrystalReports.Engine.TextObject str_phuthukhac;
                    str_phuthukhac = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_phuthukhac"];
                    str_phuthukhac.Text = decimal.Parse(_tbPhieu.Rows[0]["PHU_THU_KHAC"] + "").ToString("N0");
                    rpt.DataDefinition.FormulaFields["fphuthu"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["PHU_THU_KHAC"] + "").ToString("N0") + "'";

                    //CrystalDecisions.CrystalReports.Engine.TextObject str_thuevat;
                    //str_thuevat = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_vat"];
                    //str_thuevat.Text = decimal.Parse(_tbPhieu.Rows[0]["VAT_TIEN"] + "").ToString("N0");
                    rpt.DataDefinition.FormulaFields["fvat"].Text = "'" + decimal.Parse(_tbPhieu.Rows[0]["VAT_TIEN"] + "").ToString("N0") + "'";

                    CrystalDecisions.CrystalReports.Engine.TextObject str_vat_pt;
                    str_vat_pt = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_vat_pt"];
                    str_vat_pt.Text = "(" + decimal.Parse(_tbPhieu.Rows[0]["VAT_PHANTRAM"] + "").ToString("N0") + "%)";
                    if (str_vat_pt.Text.Trim() == "(0%)")
                        str_vat_pt.Text = "";

                    tongtien = decimal.Parse(_tbPhieu.Rows[0]["TONG_TIEN"] + "");
                    CrystalDecisions.CrystalReports.Engine.TextObject str_thanhtien;
                    str_thanhtien = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_thanhtien"];
                    str_thanhtien.Text = tongtien.ToString("N0");
                    rpt.DataDefinition.FormulaFields["fthanhtien"].Text = "'" + str_thanhtien.Text + "'";

                    Boolean PRINT_HIEN_THI_TIEN_THOI = false;
                    Boolean PRINT_HIEN_THI_TIEN_KHACH_DUA = false;
                    Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["HIEN_THI_TIEN_THOI"] + "", out PRINT_HIEN_THI_TIEN_THOI);
                    Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["HIEN_THI_TIEN_KHACH_DUA"] + "", out PRINT_HIEN_THI_TIEN_KHACH_DUA);


                    tienkhachdua = decimal.Parse(_tbPhieu.Rows[0]["TIEN_DA_NHAN"] + "");
                    tiendatcoc = decimal.Parse(_tbPhieu.Rows[0]["DAT_COC"] + "");

                    #region "show/ hide section tien khach dua va tien thoi lai"

                    CrystalDecisions.CrystalReports.Engine.Section sec_tienkhachdua;
                    sec_tienkhachdua = (CrystalDecisions.CrystalReports.Engine.Section)rpt.ReportDefinition.Sections["ReportFooterSection3"];

                    CrystalDecisions.CrystalReports.Engine.Section sec_tiendatcoc;
                    sec_tiendatcoc = (CrystalDecisions.CrystalReports.Engine.Section)rpt.ReportDefinition.Sections["ReportFooterSection5"];

                    CrystalDecisions.CrystalReports.Engine.Section sec_tienthoilai;
                    sec_tienthoilai = (CrystalDecisions.CrystalReports.Engine.Section)rpt.ReportDefinition.Sections["ReportFooterSection2"];

                    if (PRINT_HIEN_THI_TIEN_KHACH_DUA)
                    {
                        if (tienkhachdua > 0)
                        {
                            sec_tienkhachdua.SectionFormat.EnableSuppress = false;
                            if (tiendatcoc > 0)
                                sec_tiendatcoc.SectionFormat.EnableSuppress = false;
                            else
                                sec_tiendatcoc.SectionFormat.EnableSuppress = true;

                            if (PRINT_HIEN_THI_TIEN_THOI)
                                sec_tienthoilai.SectionFormat.EnableSuppress = false;
                            else
                                sec_tienthoilai.SectionFormat.EnableSuppress = true;
                        }
                        else
                        {
                            sec_tienkhachdua.SectionFormat.EnableSuppress = true;
                            sec_tiendatcoc.SectionFormat.EnableSuppress = true;
                            sec_tienthoilai.SectionFormat.EnableSuppress = true;
                        }


                    }

                    else
                    {
                        sec_tienkhachdua.SectionFormat.EnableSuppress = true;
                        sec_tiendatcoc.SectionFormat.EnableSuppress = true;
                        sec_tienthoilai.SectionFormat.EnableSuppress = true;
                    }

                    //--------------------------------------------------------

                    #endregion




                    if (tiendatcoc < 0)
                        tiendatcoc = 0;

                    //CrystalDecisions.CrystalReports.Engine.TextObject str_tiendatcoc;
                    //str_tiendatcoc = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tiendatcoc"];
                    //str_tiendatcoc.Text = tiendatcoc.ToString("N0");
                    rpt.DataDefinition.FormulaFields["ftiendatcoc"].Text = "'" + tiendatcoc.ToString("N0") + "'";

                    CrystalDecisions.CrystalReports.Engine.TextObject str_tienkhachdua;
                    str_tienkhachdua = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tienkhachdua"];
                    str_tienkhachdua.Text = tienkhachdua.ToString("N0");

                    if (Variable.tbBH_CAU_HINH.Rows[0]["HIEN_THI_TIEN_KHACH_DUA"] + "" == "True")
                        rpt.DataDefinition.FormulaFields["ftienkhachdua"].Text = "'" + tienkhachdua.ToString("N0") + "'";
                    else
                        rpt.DataDefinition.FormulaFields["ftienkhachdua"].Text = "'0'";
                    //rpt.DataDefinition.FormulaFields["ftienthua"].Text = "'" + 0 + "'";

                    // tien thoi lai 123
                    CrystalDecisions.CrystalReports.Engine.TextObject str_tienthoilai;
                    str_tienthoilai = (CrystalDecisions.CrystalReports.Engine.TextObject)rpt.ReportDefinition.ReportObjects["txt_tienthoilai"];

                    // dat coc 123

                    if (tiendatcoc + tienkhachdua > tongtien)
                    {
                        //str_tienthoilai.Text = (tiendatcoc + tienkhachdua - tongtien).ToString("N0");
                        rpt.DataDefinition.FormulaFields["ftienthua"].Text = "'" + (tiendatcoc + tienkhachdua - tongtien).ToString("N0") + "'";
                    }
                    else
                    {
                        //str_tienthoilai.Text = "0";
                        rpt.DataDefinition.FormulaFields["ftienthua"].Text = "'" + 0 + "'";
                    }
                    rpt.DataDefinition.FormulaFields["ftiendatcoc"].Text = "'" + tiendatcoc.ToString("N0") + "'";
                    #endregion


                    #region "   footer"
                    if (tbDM_MAYIN_BIENNHAN_CAUHINH.Rows.Count > 0)
                    {
                        string sFooter = tbDM_MAYIN_BIENNHAN_CAUHINH.Rows[0]["FOOTER"] + "";
                        sFooter = sFooter.Trim().Replace("\r\n", "@");
                        string[] sFooters = sFooter.Split('@');
                        for (int if1 = 0; if1 < sFooters.Length && if1 <= 3; if1++)
                        //foreach (string sfot in sFooter.Split('@'))
                        {
                            rpt.DataDefinition.FormulaFields["footer" + (if1 + 1)].Text = "'" + sFooters[if1] + "'";
                        }
                    }
                    #endregion
                }

                #region
                if (in_sohoadon == true)
                    rpt.DataDefinition.FormulaFields["fsophieu"].Text = "'Số phiếu: " + maphieu + "'";
                else
                    rpt.DataDefinition.FormulaFields["fsophieu"].Text = "''";

                for (int i = 0; i <= tb.Rows.Count - 1; i++)
                {
                    if (_tbPhieu.Rows[0]["CK_PHANTRAM"] + "" != tb.Rows[i]["CHIETKHAU_PHANTRAM"] + "")
                    {
                        if (tb.Rows[i]["CHIETKHAU_PHANTRAM"] + "" == "100")
                        {
                            tb.Rows[i]["OBJNAME"] = tb.Rows[i]["OBJNAME"] + "   -- Tặng";
                        }
                        else if (tb.Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "0" && tb.Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "")
                        {
                            tb.Rows[i]["OBJNAME"] = tb.Rows[i]["OBJNAME"] + "   -- Giảm " + tb.Rows[i]["CHIETKHAU_PHANTRAM"] + "%";
                        }
                    }
                }
                rpt.SetDataSource(tb);
                //CHIETKHAU_PHANTRAM
                rpt.DataDefinition.FormulaFields["OBJNAME"].Text = "{dt_report.OBJNAME}" + "";
                rpt.DataDefinition.FormulaFields["DON_GIA"].Text = "{dt_report.DON_GIA}" + "";

                if (ocpublic.Registry_Get("chkHien_Thi_DVT") == "True")
                    rpt.DataDefinition.FormulaFields["DVT_TEN"].Text = "{dt_report.TEN_DVT}";
                else
                    rpt.DataDefinition.FormulaFields["DVT_TEN"].Text = "' '";

                rpt.DataDefinition.FormulaFields["SO_LUONG"].Text = "{dt_report.SO_LUONG}";
                rpt.DataDefinition.FormulaFields["THANH_TIEN"].Text = "{dt_report.THANH_TIEN}" + "";
                #endregion



                //#region "             subreport device group product, editor: PL, date: 2012.06.26"

                //DataSet ds = new DataSet();
                //if (_bInlai)
                //{
                //    ds = REPrint_PHIEU_BANHANG_DATASET(maphieu, _bInlai);
                //}
                //else
                //{
                //    ds = REPrint_PHIEU_BANHANG_BILLBIENNHAN_DATASET(maphieu);
                //}

                //for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                //{
                //    if (_tbPhieu.Rows[0]["CK_PHANTRAM"] + "" != ds.Tables[0].Rows[i]["CHIETKHAU_PHANTRAM"] + "")
                //    {
                //        if (ds.Tables[0].Rows[i]["CHIETKHAU_PHANTRAM"] + "" == "100")
                //        {
                //            ds.Tables[0].Rows[i]["OBJNAME_D"] = ds.Tables[0].Rows[i]["OBJNAME_D"] + " -- Tặng";
                //        }
                //        else if (ds.Tables[0].Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "0" && ds.Tables[0].Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "")
                //        {
                //            ds.Tables[0].Rows[i]["OBJNAME_D"] = ds.Tables[0].Rows[i]["OBJNAME_D"] + " -- Giảm " + ds.Tables[0].Rows[i]["CHIETKHAU_PHANTRAM"] + "%";
                //        }
                //    }
                //}

                //for (int i = 0; i <= ds.Tables[1].Rows.Count - 1; i++)
                //{
                //    if (_tbPhieu.Rows[0]["CK_PHANTRAM"] + "" != ds.Tables[1].Rows[i]["CHIETKHAU_PHANTRAM"] + "")
                //    {
                //        if (ds.Tables[1].Rows[i]["CHIETKHAU_PHANTRAM"] + "" == "100")
                //        {
                //            ds.Tables[1].Rows[i]["OBJNAME_F"] = ds.Tables[1].Rows[i]["OBJNAME_F"] + " -- Tặng";
                //        }
                //        else if (ds.Tables[1].Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "0" && ds.Tables[1].Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "")
                //        {
                //            ds.Tables[1].Rows[i]["OBJNAME_F"] = ds.Tables[1].Rows[i]["OBJNAME_F"] + " -- Giảm " + ds.Tables[1].Rows[i]["CHIETKHAU_PHANTRAM"] + "%";
                //        }
                //    }
                //}

                //for (int i = 0; i <= ds.Tables[2].Rows.Count - 1; i++)
                //{
                //    if (_tbPhieu.Rows[0]["CK_PHANTRAM"] + "" != ds.Tables[2].Rows[i]["CHIETKHAU_PHANTRAM"] + "")
                //    {
                //        if (ds.Tables[2].Rows[i]["CHIETKHAU_PHANTRAM"] + "" == "100")
                //        {
                //            ds.Tables[2].Rows[i]["OBJNAME_O"] = ds.Tables[2].Rows[i]["OBJNAME_O"] + " -- Tặng";
                //        }
                //        else if (ds.Tables[2].Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "0" && ds.Tables[2].Rows[i]["CHIETKHAU_PHANTRAM"] + "" != "")
                //        {
                //            ds.Tables[2].Rows[i]["OBJNAME_O"] = ds.Tables[2].Rows[i]["OBJNAME_O"] + " -- Giảm " + ds.Tables[2].Rows[i]["CHIETKHAU_PHANTRAM"] + "%";
                //        }
                //    }
                //}


                //if (ds.Tables[1].Rows.Count <= 0)
                //{
                //    rpt.DataDefinition.FormulaFields["flag1"].Text = "1";//----Không cần thể hiện trên bill---07/07/2012---- 
                //}
                //rpt.Subreports["subreport1"].SetDataSource(ds.Tables[1]);
                //rpt.Subreports["subreport1"].DataDefinition.FormulaFields["OBJNAME_F"].Text = "{dt_report.OBJNAME_F}" + "";
                //rpt.Subreports["subreport1"].DataDefinition.FormulaFields["SO_LUONG_F"].Text = "{dt_report.SO_LUONG_F}" + "";
                //rpt.Subreports["subreport1"].DataDefinition.FormulaFields["DON_GIA_F"].Text = "{dt_report.DON_GIA_F}" + "";
                //rpt.Subreports["subreport1"].DataDefinition.FormulaFields["THANH_TIEN"].Text = "{dt_report.THANH_TIEN}" + "";


                //if (ds.Tables[0].Rows.Count <= 0)
                //{
                //    rpt.DataDefinition.FormulaFields["flag2"].Text = "1";//----Không cần thể hiện trên bill---07/07/2012---- 
                //}
                //rpt.Subreports["subreport2"].SetDataSource(ds.Tables[0]);
                //rpt.Subreports["subreport2"].DataDefinition.FormulaFields["OBJNAME_D"].Text = "{dt_report.OBJNAME_D}" + "";
                //rpt.Subreports["subreport2"].DataDefinition.FormulaFields["SO_LUONG_D"].Text = "{dt_report.SO_LUONG_D}" + "";
                //rpt.Subreports["subreport2"].DataDefinition.FormulaFields["DON_GIA_D"].Text = "{dt_report.DON_GIA_D}" + "";
                //rpt.Subreports["subreport2"].DataDefinition.FormulaFields["THANH_TIEN"].Text = "{dt_report.THANH_TIEN}" + "";

                //if (ds.Tables[2].Rows.Count <= 0)
                //{
                //    rpt.DataDefinition.FormulaFields["flag3"].Text = "1";//----Không cần thể hiện trên bill---07/07/2012---- 
                //}
                //rpt.Subreports["subreport3"].SetDataSource(ds.Tables[2]);
                //rpt.Subreports["subreport3"].DataDefinition.FormulaFields["OBJNAME_O"].Text = "{dt_report.OBJNAME_O}" + "";
                //rpt.Subreports["subreport3"].DataDefinition.FormulaFields["SO_LUONG_O"].Text = "{dt_report.SO_LUONG_O}" + "";
                //rpt.Subreports["subreport3"].DataDefinition.FormulaFields["DON_GIA_O"].Text = "{dt_report.DON_GIA_O}" + "";
                //rpt.Subreports["subreport3"].DataDefinition.FormulaFields["THANH_TIEN"].Text = "{dt_report.THANH_TIEN}" + "";

                //#endregion

                System.Drawing.Printing.PrinterSettings cls = new System.Drawing.Printing.PrinterSettings();
                string sPrinterDefault = cls.PrinterName;
                rpt.PrintOptions.PrinterName = sPrinterDefault;
                try
                {
                    if (ocpublic.Registry_Get("chkCrystalReport_Display") + "" == "True")
                    {
                        mh_View frm = new mh_View(rpt);
                        frm.Show();
                    }
                }
                catch (Exception ex)
                {
                }
                for (int i = 1; i <= iPrintOut; i++)
                    rpt.PrintToPrinter(1, false, 1, 100);

                if (Variable.tbBH_CAU_HINH.Rows[0]["MO_KET"] + "" == "True")
                    LPV_CASH_DRAWER.OpenCashDrawer(rpt.PrintOptions.PrinterName);
                rpt.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        public void InBill(string maphieu, int i1Epson_2Bixolon_3Bixolon100, string sTieuDe, int iPrintOut)
        {
            //for (int i = 1; i <= _iPrintOut; i++)
            f_inbiennhan(maphieu, true, i1Epson_2Bixolon_3Bixolon100, sTieuDe, iPrintOut);

        }


        #region "    in lai"
        // -------------------------------- Begin in lai bill thanh toan -------------------------------------------------------------

        DataTable REPrint_PHIEU_BANHANG(string _maPhieu, Boolean _bInlai)
        {
            tb = null;
            //   _tbPhieu = null;

            if (_bInlai)
            {
                //sPhieu = "BH_PHIEU";
                // sPhieuCT = "BH_PHIEU_CT";
                sPhieuCT = "BH_PHIEU_CT_TMP";
            }
            else
            {//TMP
                //sPhieu = "BH_PHIEU_TMP";
                //sPhieuCT = "BH_PHIEU_CT_TMP";
                sPhieuCT = "BH_PHIEU_CT";
            }


            #region "   LoadData BH_PHIEU-BH_PHIEU_CT (BH_PHIEU_TMP-BH_PHIEU_CT_TMP)"

            //  Variable.Get_DM_MAYIN_HD();

            DataRow _drPhieu = null;
            DataTable _tbPhieuCT = null;
            DataTable _tbPhieu = this.getTable_Phieu("", _maPhieu + "");

            string maPhieu = "";
            if (_tbPhieu.Rows.Count == 1)
                maPhieu = _tbPhieu.Rows[0]["OBJID"] + "";
            else
                return _tbPhieuCT = null;

            if (_tbPhieu.Select("OBJID LIKE '" + maPhieu + "'").Length > 0)
                _drPhieu = _tbPhieu.Select("OBJID LIKE '" + maPhieu + "'")[0];
            string sTien_Gio = "Tiền giờ";
            if (Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''").Length > 0)
                sTien_Gio = Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''")[0]["TIEN_PHONG_GIO"] + "";
            string sSQL = "";// "SELECT OBJNAME = (CASE b.MAHH WHEN 'Tien_Gio' THEN N'Tiền giờ' ELSE h.OBJNAME END ), b.* FROM BH_PHIEU_CT_TMP b LEFT JOIN DM_HH h ON b.MAHH = h.OBJID WHERE b.BH_PHIEU_MA LIKE '" + maPhieu + "' ORDER BY b.OBJID";
            sSQL =
                "SELECT MIN(aa.OBJID), aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, SO_LUONG = SUM(aa.SO_LUONG), aa.DVT, DON_GIA = MAX(aa.DON_GIA), " +
                "       CHIETKHAU_PHANTRAM = MAX(aa.CHIETKHAU_PHANTRAM), CHIETKHAU_TIEN = MAX(aa.CHIETKHAU_TIEN), THANH_TIEN = SUM(aa.THANH_TIEN),   " +
                "       VAT_TIEN = MAX(aa.VAT_TIEN), VAT_PHANTRAM, " +
                "       TEN_DVT = ISNULL(aa.TEN_DVT,'') " +
                "FROM  " +
                "(  " +
                "    SELECT b.OBJID, OBJNAME = (CASE b.MAHH WHEN 'Tien_Gio' THEN N'" + sTien_Gio + "' ELSE h.OBJNAME END ), b.BH_PHIEU_MA, " +
                "           b.MAHH, b.SO_LUONG, b.DVT, b.DON_GIA, b.CHIETKHAU_PHANTRAM, " +
                "           b.CHIETKHAU_TIEN, b.THANH_TIEN, b.VAT_TIEN, b.VAT_PHANTRAM, " +
                "           TEN_DVT = dd.OBJNAME " +
                "    FROM " + sPhieuCT + " b " +
                "        LEFT JOIN DM_HH h ON b.MAHH = h.OBJID  " +
                "        LEFT JOIN DM_DVT dd ON dd.OBJID = b.DVT " +
                "    WHERE b.BH_PHIEU_MA LIKE '" + maPhieu + "'  " +
                ")aa " +
                "GROUP BY aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, aa.DVT, aa.VAT_PHANTRAM, aa.TEN_DVT  " +
                "HAVING SUM(aa.SO_LUONG) > 0 " +
                "ORDER BY MIN(aa.OBJID)";
            thuvien othuvien = new thuvien();

            _tbPhieuCT = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            if (_bInlai)
            {
                //sSQL = "UPDATE " + sPhieu + " SET GHI_CHU = ISNULL(GHI_CHU, '') + N' - In lại hóa đơn " + DateTime.Now.ToString("dd/MM/yyyy mm:hh") + "' WHERE OBJID = '" + maPhieu + "'";
                //othuvien.SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new string[] { sSQL });
            }
            #endregion


            return _tbPhieuCT;

        }

        // public void InBill_Inlai(string _maPhieu, Boolean _bInlai, Boolean epson)
        public void InBill_Inlai(string _maPhieu, Boolean _bInlai, int i1esption_2bixolon_3bixolon100, string sTieuDe, int _iPrintOut)
        {
            DataTable tb = REPrint_PHIEU_BANHANG(_maPhieu, _bInlai);
            f_inbiennhan(_maPhieu, true, i1esption_2bixolon_3bixolon100, sTieuDe, _iPrintOut);
            return;

        }

        private void mh_viewthanhtoan_Load(object sender, EventArgs e)
        {

        }


        // -------------------------------- End in lai bill thanh toan -------------------------------------------------------------
        #endregion

        internal void InBill_Inlai(string BH_PHIEU_MA, bool p, int p_3)
        {
            throw new NotImplementedException();
        }



        //#region "    fix bill,          editor: PL, date 2012.06.26   "

        //DataSet REPrint_PHIEU_BANHANG_DATASET(string _maPhieu, Boolean _bInlai)
        //{
        //    tb = null;
        //    //   _tbPhieu = null;

        //    if (_bInlai)
        //    {
        //        //sPhieu = "BH_PHIEU";
        //        sPhieuCT = "BH_PHIEU_CT";
        //    }
        //    else
        //    {//TMP
        //        //sPhieu = "BH_PHIEU_TMP";
        //        sPhieuCT = "BH_PHIEU_CT_TMP";
        //    }


        //    #region "   LoadData BH_PHIEU-BH_PHIEU_CT (BH_PHIEU_TMP-BH_PHIEU_CT_TMP)"

        //    //  Variable.Get_DM_MAYIN_HD();

        //    DataRow _drPhieu = null;
        //    DataTable _tbPhieu = this.getTable_Phieu("", _maPhieu + "");
        //    DataSet _dsPhieuCT = new DataSet();

        //    string maPhieu = "";
        //    if (_tbPhieu.Rows.Count == 1)
        //        maPhieu = _tbPhieu.Rows[0]["OBJID"] + "";
        //    else
        //        return _dsPhieuCT = null;

        //    if (_tbPhieu.Select("OBJID LIKE '" + maPhieu + "'").Length > 0)
        //        _drPhieu = _tbPhieu.Select("OBJID LIKE '" + maPhieu + "'")[0];
        //    string sTien_Gio = "Tiền giờ";
        //    if (Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''").Length > 0)
        //        sTien_Gio = Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''")[0]["TIEN_PHONG_GIO"] + "";

        //    string[] s;
        //    object[] o;
        //    thuvien tv;
        //    tv = new thuvien();
        //    s = new string[] { "@maPhieu", "@sPhieuCT" };
        //    o = new object[] { maPhieu, sPhieuCT };
        //    _dsPhieuCT = new thuvien().SP_SelectDataset("SP_GETDATA_PHIEUCT_TMP_BILLTT_DATASET", s, o);

        //    #endregion
        //    return _dsPhieuCT;

        //}

        //DataSet REPrint_PHIEU_BANHANG_BILLBIENNHAN_DATASET(string _maPhieu)
        //{
        //    tb = null;
        //    //   _tbPhieu = null;


        //    #region "   LoadData BH_PHIEU-BH_PHIEU_CT (BH_PHIEU_TMP-BH_PHIEU_CT_TMP)"

        //    //  Variable.Get_DM_MAYIN_HD();

        //    DataRow _drPhieu = null;
        //    DataTable _tbPhieu = this.getTable_Phieu("", _maPhieu + "");
        //    DataSet _dsPhieuCT = new DataSet();

        //    string maPhieu = "";
        //    if (_tbPhieu.Rows.Count == 1)
        //        maPhieu = _tbPhieu.Rows[0]["OBJID"] + "";
        //    else
        //        return _dsPhieuCT = null;

        //    if (_tbPhieu.Select("OBJID LIKE '" + maPhieu + "'").Length > 0)
        //        _drPhieu = _tbPhieu.Select("OBJID LIKE '" + maPhieu + "'")[0];
        //    string sTien_Gio = "Tiền giờ";
        //    if (Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''").Length > 0)
        //        sTien_Gio = Variable.tbDM_LOAIDICHVU.Select("isRun = True AND TIEN_PHONG_GIO <> ''")[0]["TIEN_PHONG_GIO"] + "";

        //    string[] s;
        //    object[] o;
        //    thuvien tv;
        //    tv = new thuvien();
        //    s = new string[] { "@maPhieu" };
        //    o = new object[] { maPhieu };
        //    _dsPhieuCT = new thuvien().SP_SelectDataset("SP_GETDATA_PHIEUCT_TMP_BILL_DATASET", s, o);

        //    #endregion
        //    return _dsPhieuCT;

        //}


        //bc theo nhieu httt, --editor: PL,date: 2012.07.25
        private DataTable f_GetOneRow(string st_maPhieu)
        {
            string[] s = new string[] { "@flag", "@choose", "@BH_PHIEU_MA" };
            object[] o = new object[] { 3, 1, st_maPhieu };
            DataTable dt = new thuvien().SP_SelectDataTable("sp_mh_dmTheTT", s, o);
            return dt;
        }

        private DataTable f_Get_TINHTIENBANGTHE(string st_maPhieu)
        {
            string[] s = new string[] { "@flag", "@choose", "@BH_PHIEU_MA" };
            object[] o = new object[] { 3, 2, st_maPhieu };
            DataTable dt = new thuvien().SP_SelectDataTable("sp_mh_dmTheTT", s, o);
            return dt;
        }


        //#endregion
    }
}
